//
//  LTChatMomentRedDotManger.m
//  LTChat
//
//  Created by 俊松何 on 2017/8/15.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTChatMomentRedDotManger.h"
#import "LTClient.h"
static NSString *redmomoentNumberKey        = @"memontNumber";
static NSString *redmomoentNumberAvatarKey  = @"momoentNumberAvatar";


static NSString *redlastAvatarKey           = @"lastAvatar";



static NSString *redsqureAvaterKey     = @"lastfllowAvatar";
static NSString *redsqureNumber        = @"squretfllowNumber";

static NSString *redsqurelastAvatarKey         = @"squrelastavatar";

@implementation LTMomentRedDotModel



@end

@implementation LTChatMomentRedDotManger


-(NSString *)userStr:(NSString *)key
{
    return [NSString stringWithFormat:@"%@%@",key,[LTClient sharedClient].currentUserId];
}

-(NSMutableArray *)redDotdelegateArray
{
    if(_redDotdelegateArray == nil)
    {
        _redDotdelegateArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _redDotdelegateArray;
}


/**
 添加回调代理
 
 @param deleagte 要添加的代理
 */
-(void)addDelegate:(id<LTChatMomentRedDotMangerDelegate>)deleagte
{
    [self.redDotdelegateArray addObject:deleagte];
    
}

/**
 移除回调
 
 @param delegate 移除的代理
 */
-(void)removeDelegate:(id<LTChatMomentRedDotMangerDelegate>)delegate
{
    [self.redDotdelegateArray removeObject:delegate];

}


-(void)updateRedDotData:(LTMomentRedDotModel *)model
{
    switch (model.type) {
        case LTMomentRedDotTypeSelf:{
            [[NSUserDefaults standardUserDefaults] setObject:@(model.num) forKey:[self userStr:redmomoentNumberKey]];
            [[NSUserDefaults standardUserDefaults] setObject:model.avatar forKey:[self userStr:redmomoentNumberAvatarKey]];
        
        } break;
            
        case LTMomentRedDotTypeOther:
            [[NSUserDefaults standardUserDefaults] setObject:model.avatar forKey:[self userStr:redlastAvatarKey]];      break;
            
        case LTSqureRedDotTypeSelf:
        {
            [[NSUserDefaults standardUserDefaults] setObject:@(model.num)  forKey:[self userStr:redsqureNumber]];
            [[NSUserDefaults standardUserDefaults] setObject:model.avatar  forKey:[self userStr:redsqureAvaterKey]];   break;
        }
        case LTSqureRedDotTypeOther:
            [[NSUserDefaults standardUserDefaults] setObject:model.avatar forKey:[self userStr:redsqurelastAvatarKey]]; break;
            
        default: break;
    }
   
    
    for (id<LTChatMomentRedDotMangerDelegate> redmanger in self.redDotdelegateArray) {
        if(redmanger && [redmanger respondsToSelector:@selector(redDotMessageUnmberChange)]){
            [redmanger redDotMessageUnmberChange];
        }
    }
    

}

//判断朋友圈这个tabbarItem是否显示小红点
-(BOOL)showMomentTabbarRedDot
{
    NSNumber *number = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redmomoentNumberKey]];
    NSString *avtar  = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redlastAvatarKey]];
  
    
    BOOL havNumber = NO;
    BOOL havAvtar  = NO;
    
 
    
    if(number){
       havNumber =  [number integerValue];
    }
    
    if(avtar)
    {
        if(avtar.length != 0)
        {
            havAvtar = YES;
        }
    }
    return havAvtar || havNumber;
}

-(BOOL)showSquareTabbarRedDot
{
    
    NSNumber *squreNumber = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redsqureNumber]];
    NSString *squreAvtar = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redsqurelastAvatarKey]];

    BOOL havSqureNumber = NO;
    BOOL havSqureAvtar = NO;

    if(squreNumber)
    {
        havSqureNumber  = [squreNumber integerValue];
    }
    
    if(squreAvtar)
    {
        if(squreAvtar.length != 0)
        {
            havSqureAvtar = YES;
        }
    }

    return havSqureAvtar || havSqureNumber;
}



//获取评论动态数量
-(NSInteger)mommentCommentNumber
{
    NSInteger momentNumber = 0;
    
    NSNumber *number = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redmomoentNumberKey]];
    if(number)
    {
        momentNumber = [number integerValue];
    }
    
    return momentNumber;
}

//数字动态最新的头像
-(NSString *)mommentCommentLastAvatar
{
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redmomoentNumberAvatarKey]];
    
    return string;
}


//获取最新朋友发的动态
-(NSString *)latestMonmmentAvatar
{
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redlastAvatarKey]];
    
    return string;
}




//获取广场评论动态数量
-(NSInteger)squreCommentNumber
{
    NSInteger momentNumber = 0;
    
    NSNumber *number = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redsqureNumber]];
    if(number)
    {
        momentNumber = [number integerValue];
    }
    
    return momentNumber;
}

//数字广场动态最新的头像
-(NSString *)squreCommentLastAvatar
{
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redsqureAvaterKey]];
    
    return string;
}


//获取最新关注的好友发的动态
-(NSString *)latestsquretAvatar
{
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:[self userStr:redsqurelastAvatarKey]];
    
    return string;
}






//清楚评论的记录
-(void)clearMommentCommentNumber
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[self userStr:redmomoentNumberKey]];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[self userStr:redmomoentNumberAvatarKey]];
    
    for (id<LTChatMomentRedDotMangerDelegate> redmanger in self.redDotdelegateArray) {
        if(redmanger && [redmanger respondsToSelector:@selector(redDotMessageUnmberChange)]){
            [redmanger redDotMessageUnmberChange];
        }
    }
    
}

//清楚最新朋友发的动态的记录
-(void)clearlatestMonmmentAvatar
{

    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[self userStr:redlastAvatarKey]];
    
    for (id<LTChatMomentRedDotMangerDelegate> redmanger in self.redDotdelegateArray) {
        if(redmanger && [redmanger respondsToSelector:@selector(redDotMessageUnmberChange)]){
            [redmanger redDotMessageUnmberChange];
        }
    }
}

//清除广场关注用户发的动态记录
-(void)clearSqureFllowNumber
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[self userStr:redsqurelastAvatarKey]];
    
    for (id<LTChatMomentRedDotMangerDelegate> redmanger in self.redDotdelegateArray) {
        if(redmanger && [redmanger respondsToSelector:@selector(redDotMessageUnmberChange)]){
            [redmanger redDotMessageUnmberChange];
        }
    }

}

//清楚广场动态消息
-(void)clearSqureMyself
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[self userStr:redsqureAvaterKey]];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[self userStr:redsqureNumber]];

    for (id<LTChatMomentRedDotMangerDelegate> redmanger in self.redDotdelegateArray) {
        if(redmanger && [redmanger respondsToSelector:@selector(redDotMessageUnmberChange)]){
            [redmanger redDotMessageUnmberChange];
        }
    }

    

}

@end
